﻿using System;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace MushroomPicker
{
    public class SoundSetting
    {
        List<CheckBox> musicSet = new List<CheckBox>();
        List<CheckBox> soundSet = new List<CheckBox>();
        const string KeyMusic = "KeyMusic";
        const string KeySound = "KeySound";
        static SoundSetting _SoundSetting;
        public static SoundSetting Current
        {
            get
            {
                if (_SoundSetting == null)
                    _SoundSetting = new SoundSetting();
                return _SoundSetting;
            }
        }
        private SoundSetting()
        {
            Load();
            
        }
        void Load()
        {
            var s = IsolatedStorageSettings.ApplicationSettings;
            if (s.Contains(KeyMusic))
            {
                SetIsMusic((bool)s[KeyMusic]);
            }
            else
            {
                s.Add(KeyMusic, Sound.IsMusicPlay);
            }
            if (s.Contains(KeySound))
            {
                SetIsSound((bool)s[KeySound]);
            }
            else
            {
                s.Add(KeySound, Sound.IsEffectSound);
            }
        }
        public void SetIsMusic(bool ismusic,bool save = false)
        {
            Sound.IsMusicPlay = ismusic;
            foreach (var item in musicSet)
            {
                item.IsChecked = ismusic;
            }
            if (save)
            {
                var s = IsolatedStorageSettings.ApplicationSettings;
                if (s.Contains(KeyMusic))
                {
                    s[KeyMusic] = Sound.IsMusicPlay;
                }
                else
                {
                    s.Add(KeyMusic, Sound.IsMusicPlay);
                }
                s.Save();
            }
        }
        public void SetIsSound(bool isound,bool save = false)
        {
            Sound.IsEffectSound = isound;
            foreach (var item in soundSet)
            {
                item.IsChecked = isound;
            }
            if (save)
            {
                var s = IsolatedStorageSettings.ApplicationSettings;
                if (s.Contains(KeySound))
                {
                    s[KeySound] = Sound.IsEffectSound;
                }
                else
                {
                    s.Add(KeySound, Sound.IsEffectSound);
                }
                s.Save();
            }

        }
        public void AddMusicSet(CheckBox set)
        {
            set.Checked += new RoutedEventHandler(MusicSet_Checked);
            set.Unchecked += new RoutedEventHandler(MusicSet_Unchecked);
            //musicSet.Add(set);
            set.IsChecked = Sound.IsMusicPlay;
        }

        void MusicSet_Unchecked(object sender, RoutedEventArgs e)
        {
            Sound.IsMusicPlay = false;
            SetIsMusic(false,true);

        }

        void MusicSet_Checked(object sender, RoutedEventArgs e)
        {
            Sound.IsMusicPlay = true;
            SetIsMusic(true, true);
        }
        public void AddSoundSet(CheckBox set)
        {
            set.Checked += new RoutedEventHandler(SoundSet_Checked);
            set.Unchecked += new RoutedEventHandler(SoundSet_Unchecked);
            //soundSet.Add(set);
            set.IsChecked = Sound.IsEffectSound;
        }
        void SoundSet_Unchecked(object sender, RoutedEventArgs e)
        {
            Sound.IsEffectSound = false;
            SetIsSound(false, true);
        }

        void SoundSet_Checked(object sender, RoutedEventArgs e)
        {
            Sound.IsEffectSound = true;
            SetIsSound(true, true);
        }
    }
}
